import { Module } from 'vuex'
import { ISystemState } from '@/store/main/system/types'
import { IRootState } from '@/store/types'
import {
  createPageData,
  deletePageData,
  getPageListData,
  updatePageData
} from '@/service/main/system/system'

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      userList: [],
      userCount: 0,
      roleList: [],
      roleCount: 0,
      goodList: [],
      goodCount: 0,
      menuList: [],
      menuCount: 0
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]
        // switch (pageName) {
        //   case 'user':
        //     return state.userList
        //   case 'role':
        //     return state.roleList
        // }
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]
      }
    }
  },
  mutations: {
    changeUserList(state, list: any[]) {
      state.userList = list
    },
    changeUserCount(state, count: number) {
      state.userCount = count
    },
    changeRoleList(state, roleList: any[]) {
      state.roleList = roleList
    },
    changeRoleCount(state, count: number) {
      state.roleCount = count
    },
    changeGoodList(state, goodList: any[]) {
      state.goodList = goodList
    },
    changeGoodCount(state, count: number) {
      state.goodCount = count
    },
    changeMenuList(state, menuList: any[]) {
      state.menuList = menuList
    },
    changeMenuCount(state, count: number) {
      state.menuCount = count
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      // 通过配置pageName来获取不同页面的数据\
      const pageName = payload.pageName
      const pageUrl = `/${payload.pageName}/list`
      //发送页面请求，请求数据
      const pageListResult = await getPageListData(pageUrl, payload.queryInfo)
      // console.log(pageListResult)
      const { list, totalCount } = pageListResult.data
      //pageName.slice(0, 1).toUpperCase() + pageName.slice(1)  ===>首字母大写
      const name = pageName.slice(0, 1).toUpperCase() + pageName.slice(1)
      commit(`change${name}List`, list)
      commit(`change${name}Count`, totalCount)
      // switch (pageName) {
      //   case 'user':
      //     commit('changeUserList', userList)
      //     break
      //   case 'role':
      //     commit('changeRoleList', roleList)
      // }
    },
    async deletePageDataAction({ dispatch }, payload: any) {
      const { pageName, id } = payload
      const pageUrl = `/${pageName}/${id}`
      await deletePageData(pageUrl)
      //删除数据后需要重新请求数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },

    async createPageDataAction({ dispatch }, payload: any) {
      const { pageName, newData } = payload
      const pageUrl = `/${pageName}`
      await createPageData(pageUrl, newData)
      //新增数据后需要重新请求数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    async updatePageDataAction({ dispatch }, payload: any) {
      const { pageName, editData, id } = payload
      const pageUrl = `/${pageName}/${id}`
      await updatePageData(pageUrl, editData)
      //新增数据后需要重新请求数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    }
  }
}
export default systemModule
